package com.cskaoyan.service.impl;


import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.IssueCreateBO;
import com.cskaoyan.bean.bo.IssueListBO;
import com.cskaoyan.bean.pojo.MallIssue;
import com.cskaoyan.bean.pojo.MallIssueExample;
import com.cskaoyan.bean.vo.IssueCreateVO;
import com.cskaoyan.bean.vo.IssueListVO;
import com.cskaoyan.mapper.MallIssueMapper;
import com.cskaoyan.service.IssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: ycb
 * @description: 通用问题
 * @time: 2021/7/9 15:38
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    MallIssueMapper issueMapper;

    /**
     * @Author ycb
     * @description: 删除问题
     * @Date 20:11 2021/7/9
     * url: http://182.92.235.201:8083/admin/issue/delete
     */
    @Override
    public void deleteIssue(MallIssue mallIssueBo) {
        issueMapper.deleteByPrimaryKey(mallIssueBo.getId());
    }



    /**
     * @Author ycb
     * @description: 编辑问题
     * @Date 19:33 2021/7/9
     * url:   http://182.92.235.201:8083/admin/issue/update
     */
    @Override
    public MallIssue issueUpdate(MallIssue mallIssueBo) {
        MallIssue mallIssueVo = new MallIssue(mallIssueBo.getId(),mallIssueBo.getQuestion(),mallIssueBo.getAnswer(),mallIssueBo.getAddTime(),new Date(),false);
        issueMapper.updateByPrimaryKey(mallIssueBo);
        return mallIssueVo;
    }

    /**
     * @Author ycb
     * @description: 添加问题
     * @Date 17:02 2021/7/9
     * url:http://182.92.235.201:8083/admin/issue/create
     */
    @Override
    public IssueCreateVO CreatIssue(IssueCreateBO createBO) { ;

        int insert = issueMapper.insert(new MallIssue(null, createBO.getQuestion(), createBO.getAnswer(), new Date(), new Date(), false));
        int id = issueMapper.selectMaxId();
        MallIssue mallIssue = issueMapper.selectByPrimaryKey(id);

        IssueCreateVO issueCreateVO = new IssueCreateVO(mallIssue.getAddTime(),mallIssue.getAnswer(),mallIssue.getId(),mallIssue.getQuestion(),mallIssue.getUpdateTime());

        return issueCreateVO;
    }

    /**
     * @Author ycb
     * @description: 获取通用问题列表
     * @Date 16:10 2021/7/9
     * url: http://182.92.235.201:8083/admin/issue/list?page=1&limit=20&question=11&sort=add_time&order=desc
     */
    @Override
    public BaseData<MallIssue> getIssueList(IssueListBO issueListBO) {

        //分页
        PageHelper.startPage(issueListBO.getPage(),issueListBO.getLimit());

        //example
        MallIssueExample issueExample = new MallIssueExample();
        issueExample.setOrderByClause( issueListBO.getSort() + " " + issueListBO.getOrder());
        MallIssueExample.Criteria criteria = issueExample.createCriteria();
        if (issueListBO.getQuestion() != null && !"".equals(issueListBO.getQuestion())){
            criteria.andQuestionLike("%" + issueListBO.getQuestion() + "%");
        }

        criteria.andDeletedEqualTo(false);
        List<MallIssue> mallIssues = issueMapper.selectByExample(issueExample);

        //获得分页信息
        PageInfo<MallIssue> pageInfo = new PageInfo<>(mallIssues);
        long total = pageInfo.getTotal();

        return BaseData.ok(mallIssues,total);
    }
}